# coding: utf-8

from marshmallow_sqlalchemy import ModelSchemaOpts

from .extensions import ma, db


class BaseOpts(ModelSchemaOpts):
    """
    create by chenhj
    解决schema库找不到session的问题
    参考: https://marshmallow-sqlalchemy.readthedocs.io/en/latest/recipes.html
    """
    def __init__(self, meta):
        if not hasattr(meta, 'sql_session'):
            meta.sqla_session = db.session
        super(BaseOpts, self).__init__(meta)


class BaseModelSchema(ma.ModelSchema, ma.Schema):
    OPTIONS_CLASS = BaseOpts
